
// UstavServerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "UstavServer.h"
#include "AboutDlg.h"
#include "UstavServerDlg.h"
#include "afxdialogex.h"

#include "UstavServerSocket.h"

#include <Vfw.h>

#define CAMERA_TIMER_ID 1

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CUstavServerDlg dialog
CUstavServerDlg::CUstavServerDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CUstavServerDlg::IDD, pParent)
    , m_imageWidth(640), m_imageHeight(480), m_cameraUpdateFrequency(40)
{
    m_bmpInfo.bmiHeader.biSize		    = sizeof(BITMAPINFOHEADER);
    m_bmpInfo.bmiHeader.biWidth		    = m_imageWidth;
    m_bmpInfo.bmiHeader.biHeight		= m_imageHeight;
    m_bmpInfo.bmiHeader.biPlanes		= 1;
    m_bmpInfo.bmiHeader.biBitCount		= 32;
    m_bmpInfo.bmiHeader.biCompression	= BI_RGB;
    m_bmpInfo.bmiHeader.biSizeImage		= 0;
    m_bmpInfo.bmiHeader.biXPelsPerMeter	= 0;
    m_bmpInfo.bmiHeader.biYPelsPerMeter	= 0;
    m_bmpInfo.bmiHeader.biClrUsed		= 0;
    m_bmpInfo.bmiHeader.biClrImportant	= 0;

	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CUstavServerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CUstavServerDlg, CDialogEx)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
    ON_WM_TIMER()
END_MESSAGE_MAP()

// CUstavServerDlg message handlers

BOOL CUstavServerDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
    
    // Create camera window and attach it to existin one
    m_hCamWindow = capCreateCaptureWindow(_T("handle"), WS_CHILD | WS_VISIBLE, 0, 0, m_imageWidth, m_imageHeight, this->m_hWnd, 1);

    CRect formRect;
    this->GetWindowRect(formRect);
    this->MoveWindow(formRect.left, formRect.top, m_imageWidth, m_imageHeight);

    // Connect the driver with the webcam
    if (capDriverConnect(m_hCamWindow, 0) == false)
    {
        AfxMessageBox(_T("Webcam not found!"), 0, 0);
        return false;
    }

    SetTimer(CAMERA_TIMER_ID, m_cameraUpdateFrequency, 0);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CUstavServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.
void CUstavServerDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CUstavServerDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CUstavServerDlg::OnTimer(UINT_PTR nIDEvent)
{
    // TODO: Add your message handler code here and/or call default
    if(nIDEvent == CAMERA_TIMER_ID)
    {
        capGrabFrame(m_hCamWindow);
    }
    
    CDialogEx::OnTimer(nIDEvent);
}
